package cn.servlet;

import cn.dao.StaffDao;
import cn.dao.StaffDaoImpl;
import cn.entity.Staff;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/update")
public class UpdateServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req,
                           HttpServletResponse resp)
            throws IOException {
        req.setCharacterEncoding("utf-8");
        String idString = req.getParameter("id");
        String salaryString = req.getParameter("salary");
        // 检查id和salary是否为null
        int id = (idString != null && !idString.isEmpty()) ? Integer.parseInt(idString) : 0;
        int salary = (salaryString != null && !salaryString.isEmpty()) ? Integer.parseInt(salaryString) : 0;

        String name = req.getParameter("name");
        String job = req.getParameter("job");
        StaffDao dao = new StaffDaoImpl();
        Staff staff = new Staff(id, name, job, salary);
        dao.updateId(staff);
        resp.sendRedirect("staff_list");
    }
}

